package com.zjw.zy.coreui.view

import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.utils.AppUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/28 19:01
 *
 * 底部导航栏
 */
class FootBarView : BaseViewGroup {

    companion object {
        const val NAV_1 = "NAV_1"
        const val NAV_2 = "NAV_2"
        const val NAV_3 = "NAV_3"
        const val NAV_4 = "NAV_4"
    }

    private val mControllerNavListener =
        NavController.OnDestinationChangedListener { controller, destination, arguments ->
            when (destination.id) {
                R.id.nav_weather_15_fragment,
                R.id.nav_main_fragment -> {
                    onlySelect(mNav1)
                }
                R.id.nav_memorandum_text_fragment,
                R.id.nav_memorandum_main_fragment -> {
                    onlySelect(mNav2)
                }

                R.id.nav_wechat_fragment -> {
                    onlySelect(mNav3)
                }
                R.id.nav_browser_fragment,
                R.id.nav_tools_nav_fragment -> {
                    onlySelect(mNav4)
                }

                else -> {
                    onlySelect(null)
                }
            }
        }

    private val mNav1: ImageView by lazy { findViewById(R.id.footer_nav_1) }
    private val mNav2: ImageView by lazy { findViewById(R.id.footer_nav_2) }
    private val mNav3: ImageView by lazy { findViewById(R.id.footer_nav_3) }
    private val mNav4: ImageView by lazy { findViewById(R.id.footer_nav_4) }
    private val mNavViewList: MutableList<ImageView> = ArrayList(4)

    private var mListener: FootBarListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.footer_bar_layout
    }

    override fun onBindListener() {
        mNav1.setOnClickListener {
            mListener?.onClickFootBarItem(NAV_1)
        }
        mNav2.setOnClickListener {
            mListener?.onClickFootBarItem(NAV_2)
        }
        mNav3.setOnClickListener {
            mListener?.onClickFootBarItem(NAV_3)
        }
        mNav4.setOnClickListener {
            mListener?.onClickFootBarItem(NAV_4)
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()

        AppUtils.getMainActivity().navigationController()
            .addOnDestinationChangedListener(mControllerNavListener)
    }

    override fun onDetachedFromWindow() {
        AppUtils.getMainActivity().navigationController()
            .removeOnDestinationChangedListener(mControllerNavListener)
        super.onDetachedFromWindow()
    }

    private fun onlySelect(view: View?) {
        for (navView in mNavViewList) {
            navView.isSelected = navView == view
        }
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return false
    }


    override fun onBindValue() {
        //默认选中第一个
        onlySelect(mNav1)
    }

    override fun onFindView(rootView: View) {
        mNavViewList.add(mNav1)
        mNavViewList.add(mNav2)
        mNavViewList.add(mNav3)
        mNavViewList.add(mNav4)
    }

    fun bindListener(listener: FootBarListener) {
        mListener = listener
    }

    interface FootBarListener {
        fun onClickFootBarItem(item: String)
    }
}